<?php

class Default_Model_ArticleList
{

    public $articlesets;
    public $groupname;
    public $dataURI;
    public $Param;
    function __construct($groupname=NULL, $filter=NULL, $param=NULL)
    {
        $this->Param  = $param;
        $this->Filter = $filter;
        if ($groupname) {
            $this->groupname = $groupname;
            $this->DataURI = DATA_PATH . "/index/{$groupname}.xml";
            $this->Populate ();
        }
    }

    function Populate ()
    {
        $articleList = simplexml_load_file($this->DataURI); 
        $this->articlesets = array();  
        $query = "//list/article";
        if ($this->Filter && strlen ($this->Filter) > 0) 
            $query .= "[type='{$this->Filter}']";
        if ($this->Param && strlen ($this->Param) > 0) 
            $query .= "[contains(subject, '{$this->Param}')]";
        $result = $articleList->xpath($query);  
        for($x=0;$x<sizeof($result);$x++)
        { 
            $this->articlesets[] = new Default_Model_ArticleSet($result[$x]);
        }
    }

    function GetArticleSet($index, $sort=NULL)
    {
        if (strlen($index) > 0) {
            $articleList = simplexml_load_file($this->DataURI);
            $result = $articleList->xpath("//list/article[@index='{$index}']");  
            if (sizeof($result) == 0) {
                echo "ERROR: //list/article[@index='{$index}'] not found in '{$this->DataURI}'!";
                exit;
            }
            for($x=0;$x<sizeof($result);$x++)
            {  
                $set = new Default_Model_ArticleSet($result[$x]); 
                if ($sort) usort($set->items, 'cmp');
                return $set;
            }

        }
    } 
}
# NOTE: move to index file
function cmp($a, $b) { 
    return ($a->subject < $b->subject) ? -1 : 1;
}

